home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 January: Mac OS SDK / Dev.CD Jan 96 SDK / Dev.CD Jan 96 SDK1.toast / Development Kits (Disc 1) / OpenTransport / Open Transport 1.0.5b4 / Open Transport SDK / Open Tpt Module Developer / Includes / OpenTptDev.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-24  |  22.6 KB  |  841 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OpenTptDev.h
  3.  
  4.     Contains:    Equates for Open Transport module development
  5.  
  6.     Copyright:    © 1993-1995 by Apple Computer, Inc. and Mentat Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #ifndef __OPENTPTDEV__
  11. #define __OPENTPTDEV__
  12.  
  13. #ifndef __OPENTRANSPORT__
  14. #include <OpenTransport.h>
  15. #endif
  16. #ifndef _MPS_STREAM_
  17. #include <mistream.h>
  18. #endif
  19.  
  20. /*******************************************************************************
  21. ** Some typedefs
  22. ********************************************************************************/
  23.  
  24. typedef void* (* _CDECL OTAllocMemProcPtr)(size_t);
  25. typedef void (* _CDECL OTFreeMemProcPtr)(void*);
  26.  
  27. #define MI_BIG_ENDIAN            1
  28. #undef    MI_LITTLE_ENDIAN
  29.  
  30. //
  31. // For static members that need to match pascal functions
  32. //
  33. #define _FSDECL                static pascal
  34. #define _FSDEF                pascal
  35. #define _MDEF                /* %%% Temporary */
  36.  
  37. /*******************************************************************************
  38. **    Time functions
  39. **    
  40. **    OTGetTimeStamp returns time in "tick" numbers, stored in 64 bits.
  41. **    This timestamp can be used as a base number for calculating elapsed 
  42. **    time.
  43. **    OTSubtractTimeStamps returns a pointer to the "result" parameter.
  44. **        
  45. **    OTGetClockTimeInSecs returns time since Open Transport was initialized
  46. **        in seconds.
  47. ********************************************************************************/
  48.     
  49.     typedef UnsignedWide    OTTimeStamp;
  50.     
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54.  
  55.     void            OTGetTimeStamp(OTTimeStamp*);
  56.     OTTimeStamp*    OTSubtractTimeStamps(OTTimeStamp* result, OTTimeStamp* start,
  57.                                          OTTimeStamp* end);
  58.     UInt32            OTTimeStampInMilliseconds(OTTimeStamp* delta);
  59.     UInt32            OTTimeStampInMicroseconds(OTTimeStamp* delta);
  60.     UInt32            OTElapsedMilliseconds(OTTimeStamp* startTime);
  61.     UInt32            OTElapsedMicroseconds(OTTimeStamp* startTime);
  62.  
  63.     UInt32            OTGetClockTimeInSecs(void);
  64.     UInt32            OTGetTimerTicks(void);
  65.  
  66. #ifdef __cplusplus
  67. }
  68. #endif
  69.  
  70. /*******************************************************************************
  71. ** OTLIFO
  72. **
  73. ** These are functions to implement a LIFO list that is interrupt-safe.  The only
  74. ** function which is not is OTReverseList.  Normally, you create a LIFO list,
  75. ** populate it at interrupt time, and then use OTLIFOStealList to atomically
  76. ** remove the list, and OTReverseList to flip the list so that it is a FIFO
  77. ** list, which tends to be more useful.
  78. ********************************************************************************/
  79.  
  80. typedef struct OTLink    OTLink;
  81. typedef struct OTLIFO    OTLIFO;
  82.  
  83. struct OTLink
  84. {
  85.     OTLink*    fNext;
  86.     
  87. #ifdef __cplusplus
  88.             void    Init()
  89.                         { fNext = NULL; }
  90. #endif
  91. };
  92.  
  93. #ifdef __cplusplus
  94. extern "C" {
  95. #endif
  96.     //
  97.     // This function puts "object" on the listHead, and places the
  98.     // previous value at listHead into the pointer at "object" plus
  99.     // linkOffset.
  100.     //
  101. void    OTEnqueue(void** listHead, void* object, size_t linkOffset);
  102.     //
  103.     // This function returns the head object of the list, and places
  104.     // the pointer at "object" + linkOffset into the listHead
  105.     //
  106. void*    OTDequeue(void** listHead, size_t linkOffset);
  107.     //
  108.     // This function atomically enqueues the link onto the list
  109.     //
  110. void     OTLIFOEnqueue(OTLIFO*, OTLink*);
  111.     //
  112.     // This function atomically dequeues the first element
  113.     // on the list
  114.     //
  115. OTLink* OTLIFODequeue(OTLIFO* list);
  116.     //
  117.     // This function atomically empties the list and returns a
  118.     // pointer to the first element on the list
  119.     //
  120. OTLink*    OTLIFOStealList(OTLIFO*);
  121.     //
  122.     // This function reverses a list that was stolen by
  123.     // OTLIFOStealList.  It is NOT atomic.  It returns the
  124.     // new starting list.
  125.     //
  126. OTLink*    OTReverseList(OTLink*);
  127.  
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131.  
  132. struct OTLIFO
  133. {
  134.     OTLink*    fHead;
  135.     
  136. #ifdef __cplusplus
  137.                 void    Init()    
  138.                             { fHead = NULL; }
  139.                 
  140.                 void    Enqueue(OTLink* link)
  141.                             { OTLIFOEnqueue(this, link); }
  142.                             
  143.                 OTLink*    Dequeue()
  144.                             { return OTLIFODequeue(this); }
  145.                             
  146.                 OTLink*    StealList()
  147.                             { return OTLIFOStealList(this); }
  148.                             
  149.                 Boolean    IsEmpty()
  150.                             { return fHead == NULL; }
  151. #endif
  152. };
  153.  
  154. /*******************************************************************************
  155. ** OTList
  156. **
  157. ** An OTList is a non-interrupt-safe list, but has more features than the
  158. ** OTLIFO list.  It is a doubly-linked list that is in a ring configuration.
  159. ** Therefore, do not attempt to walk the list manually, since there is no
  160. ** NULL pointer terminating the list, and also be sure to call OTInitList
  161. ** on the list or the results will be unpleasant.
  162. ********************************************************************************/
  163.  
  164. typedef struct OTList    OTList;
  165.  
  166. typedef Boolean (* _CDECL OTListSearchProcPtr)(const void* ref, OTLink* linkToCheck);
  167.  
  168. #ifdef __cplusplus
  169. extern "C" {
  170. #endif
  171.     //
  172.     // Add the link to the list at the front
  173.     //
  174. void         OTAddFirst(OTList*, OTLink*);
  175.     //
  176.     // Add the link to the list at the end
  177.     //
  178. void         OTAddLast(OTList*, OTLink*);
  179.     //
  180.     // Remove the first link from the list
  181.     //
  182. OTLink*        OTRemoveFirst(OTList*);
  183.     //
  184.     // Remove the last link from the list
  185.     //
  186. OTLink*        OTRemoveLast(OTList*);
  187.     //
  188.     // Return the first link from the list
  189.     //
  190. OTLink*        OTGetFirst(OTList*);
  191.     //
  192.     // Return the last link from the list
  193.     //
  194. OTLink*        OTGetLast(OTList*);
  195.     //
  196.     // Return true if the link is present in the list
  197.     //
  198. Boolean        OTIsInList(OTList*, OTLink*);
  199.     //
  200.     // Find a link in the list which matches the search criteria
  201.     // established by the search proc and the refPtr.  This is done
  202.     // by calling the search proc, passing it the refPtr and each
  203.     // link in the list, until the search proc returns true.
  204.     // NULL is returned if the search proc never returned true.
  205.     //
  206. OTLink*        OTFindLink(OTList*, OTListSearchProcPtr, const void* refPtr);
  207.     //
  208.     // Remove the specified link from the list, returning true if it was found
  209.     //
  210. Boolean        OTRemoveLink(OTList*, OTLink*);
  211.     //
  212.     // Similar to OTFindLink, but it also removes it from the list.
  213.     //
  214. OTLink*        OTFindAndRemoveLink(OTList*, OTListSearchProcPtr, const void* refPtr);
  215.     //
  216.     // Return the "index"th link in the list
  217.     //
  218. OTLink*        OTGetIndexedLink(OTList*, size_t index);
  219.  
  220. #ifdef __cplusplus
  221. }
  222. #endif
  223.  
  224. struct OTList
  225. {
  226.     OTLink*        fHead;
  227.     
  228. #ifdef __cplusplus
  229.             void        Init()    
  230.                             { fHead = NULL; }
  231.             
  232.             Boolean        IsEmpty()
  233.                             { return fHead == NULL; }
  234.                             
  235.             void        AddFirst(OTLink* link)
  236.                             { OTAddFirst(this, link); }
  237.             
  238.             void        AddLast(OTLink* link)
  239.                             { OTAddLast(this, link); }
  240.             
  241.             OTLink*        GetFirst()
  242.                             { return OTGetFirst(this); }
  243.             
  244.             OTLink*        GetLast()
  245.                             { return OTGetLast(this); }
  246.             
  247.             OTLink*        RemoveFirst()
  248.                             { return OTRemoveFirst(this); }
  249.             
  250.             OTLink*        RemoveLast()
  251.                             { return OTRemoveLast(this); }
  252.                             
  253.             Boolean        IsInList(OTLink* link)
  254.                             { return OTIsInList(this, link); }
  255.                             
  256.             OTLink*        FindLink(OTListSearchProcPtr proc, const void* ref)
  257.                             { return OTFindLink(this, proc, ref); }
  258.                             
  259.             Boolean        RemoveLink(OTLink* link)
  260.                             { return OTRemoveLink(this, link); }
  261.                             
  262.             OTLink*        RemoveLink(OTListSearchProcPtr proc, const void* ref)
  263.                             { return OTFindAndRemoveLink(this, proc, ref); }
  264.                             
  265.             OTLink*        GetIndexedLink(size_t index)
  266.                             { return OTGetIndexedLink(this, index); }
  267. #endif
  268. };
  269.  
  270. #define OTGetLinkObject(link, struc, field)    \
  271.     ((struc*)((char*)(link) - offsetof(struc, field)))
  272.  
  273. /*******************************************************************************
  274. ** Hash List
  275. ********************************************************************************/
  276.  
  277. typedef struct OTHashList    OTHashList;
  278.  
  279. typedef UInt32 (* _CDECL OTHashProcPtr)(OTLink* linkToHash);
  280. typedef Boolean (* _CDECL OTHashSearchProcPtr)(const void* ref, OTLink* linkToCheck);
  281.  
  282. #ifdef __cplusplus
  283. extern "C" {
  284. #endif
  285.     //
  286.     // Return the number of bytes of memory needed to create a hash list
  287.     // of at least "numEntries" entries.
  288.     //
  289. size_t        OTCalculateHashListMemoryNeeds(size_t numEntries);
  290.     //
  291.     // Create an OTHashList from "memory".  Return an error if it
  292.     // couldn't be done.
  293.     //
  294. OTResult    OTInitHashList(void* memory, size_t numBytes, OTHashProcPtr);
  295. void        OTAddToHashList(OTHashList*, OTLink*);
  296. Boolean        OTRemoveLinkFromHashList(OTHashList*, OTLink*);
  297. Boolean        OTIsInHashList(OTHashList*, OTLink*);
  298. OTLink*        OTFindInHashList(OTHashList*, OTHashSearchProcPtr proc,
  299.                             const void* refPtr, UInt32 hashValue);
  300. OTLink*        OTRemoveFromHashList(OTHashList*, OTHashSearchProcPtr proc,
  301.                                  const void* refPtr, UInt32 hashValue);
  302.  
  303. #ifdef __cplusplus
  304. }
  305. #endif
  306.  
  307. struct OTHashList
  308. {
  309.     OTHashProcPtr        fHashProc;
  310.     size_t                fHashTableSize;
  311.     OTLink**            fHashBuckets;
  312.  
  313. #ifdef __cplusplus
  314.             void        Add(OTLink* toAdd)
  315.                             { OTAddToHashList(this, toAdd); }
  316.                             
  317.             Boolean        RemoveLink(OTLink* toRemove)
  318.                             { return OTRemoveLinkFromHashList(this, toRemove); }
  319.                             
  320.             OTLink*        Remove(OTHashSearchProcPtr proc,
  321.                                const void* refPtr, UInt32 hashValue)
  322.                             { return OTRemoveFromHashList(this, proc, refPtr, hashValue); }
  323.                             
  324.             Boolean        IsInList(OTLink* toFind)
  325.                             { return OTIsInHashList(this, toFind); }
  326.                             
  327.             OTLink*        FindLink(OTHashSearchProcPtr proc, const void* refPtr,
  328.                                  UInt32 hash)
  329.                             {    return OTFindInHashList(this, proc, refPtr, hash); }
  330. #endif
  331. };
  332.  
  333. /*******************************************************************************
  334. ** Random functions
  335. ********************************************************************************/
  336.  
  337. #ifdef __cplusplus
  338. extern "C" {
  339. #endif
  340.  
  341. UInt32    OTGetRandomSeed();
  342. UInt32    OTGetRandomNumber(UInt32* seed, UInt32 lo, UInt32 hi);
  343.  
  344. #ifdef __cplusplus
  345. }
  346. #endif
  347.  
  348. /*******************************************************************************
  349. ** Bitmap Functions
  350. ********************************************************************************/
  351.  
  352. #ifdef __cplusplus
  353. extern "C" {
  354. #endif
  355.     //
  356.     // Set the first clear bit in "bitMap", starting with bit "startBit",
  357.     // giving up after "numBits".  Returns the bit # that was set, or
  358.     // a kOTNotFoundErr if there was no clear bit available
  359.     //
  360. OTResult    OTSetFirstClearBit(UInt8* bitMap, size_t startBit, size_t numBits);
  361.     //
  362.     // Standard clear, set and test bit functions
  363.     //
  364. Boolean        OTClearBit(UInt8* bitMap, size_t bitNo);
  365. Boolean        OTSetBit(UInt8* bitMap, size_t bitNo);
  366. Boolean        OTTestBit(UInt8* bitMap, size_t bitNo);
  367.  
  368. #ifdef __cplusplus
  369. }
  370. #endif
  371.  
  372.  
  373. /*******************************************************************************
  374. ** Memory functions
  375. ********************************************************************************/
  376.  
  377. typedef void* OTVoidPtr;
  378. typedef OTVoidPtr (*_CDECL OTAllocateProcPtr)(size_t);
  379. typedef void (*_CDECL OTFreeProcPtr)(void*);
  380.  
  381. #ifdef __cplusplus
  382. extern "C" {
  383. #endif
  384.  
  385. void    OTMemcpy(void* dest, const void* src, size_t nBytes);
  386. Boolean    OTMemcmp(const void* mem1, const void* mem2, size_t nBytes);
  387. void    OTMemmove(void* dest, const void* src, size_t nBytes);
  388. void    OTMemzero(void* dest, size_t nBytes);
  389. void    OTMemset(void* dest, UInt8 toSet, size_t nBytes);
  390. size_t    OTStrLength(const char*);
  391. void    OTStrCopy(char*, const char*);
  392. void    OTStrCat(char*, const char*);
  393. Boolean    OTStrEqual(const char*, const char*);
  394.  
  395. #ifdef __cplusplus
  396. }
  397. #endif
  398.  
  399. /*******************************************************************************
  400. ** OTBuffer
  401. **     
  402. ** This is the structure that is obtained when reading messages from the
  403. ** streamhead.  When you are done with it, you must call the
  404. ** OTReleaseBuffer function.
  405. ********************************************************************************/
  406.  
  407. typedef struct OTBuffer    OTBuffer;
  408.  
  409. struct OTBuffer
  410. {
  411.         OTLink        fLink;        // b_next & b_prev
  412.         OTLink        fLink2;
  413.         OTBuffer*    fNext;        // b_cont
  414.         UInt8*        fData;        // b_rptr
  415.         size_t        fLen;        // b_wptr
  416.         void*        fSave;        // b_datap
  417.         UInt8        fBand;        // b_band
  418.         UInt8        fType;        // b_pad1
  419.         UInt8        fFlags;        // b_flag
  420.         UInt8        fPad1;
  421. };
  422.  
  423.  
  424. /*******************************************************************************
  425. ** XmitRecord Class
  426. ** 
  427. ** This class is used when sending "write"-type commands, where we need
  428. ** information to be able to complete a write to the client when it is 
  429. ** finished.
  430. ********************************************************************************/
  431.  
  432. struct XmitRecord
  433. {
  434.         OTLink        fLink;
  435.         void*        provider;
  436.         void*        dataBuf;
  437.         size_t        dataLen;
  438.         SInt16        ackCount;
  439. };
  440.     
  441. /*******************************************************************************
  442. ** Miscellaneous functions
  443. ********************************************************************************/
  444.  
  445. #ifdef __cplusplus
  446. extern "C" {
  447. #endif
  448. //
  449. // Return true if we are at interrupt level
  450. //
  451. Boolean        OTIsAtInterruptLevel(void);
  452. //
  453. // Return true if we are currently at either system task time, or we
  454. // are running a deferred task procedure from system task time.
  455. //
  456. Boolean     OTCanLoadLibraries(void);
  457. void        OTEnterCriticalSection(void);
  458. void        OTLeaveCriticalSection(void);
  459.  
  460. #ifdef __cplusplus
  461. }
  462. #endif
  463.  
  464. /*******************************************************************************
  465. ** Port ref functions
  466. ********************************************************************************/
  467.  
  468. /*    -------------------------------------------------------------------------
  469.     Port state change requests
  470.     ------------------------------------------------------------------------- */
  471.  
  472.     enum
  473.     {
  474.         kOTDisablePort    = 1,    // No new streams 
  475.         kOTClosePort    = 2,    // Force closure of all port clients
  476.         kOTEnablePort    = 3        // Undo the 2 above
  477.     };
  478.  
  479. #ifdef __cplusplus
  480. extern "C" {
  481. #endif
  482.     
  483.     pascal OTPortRef    OTSetDeviceTypeInPortRef(OTPortRef ref, UInt16 devType);
  484.     pascal OTPortRef    OTSetBusTypeInPortRef(OTPortRef ref, UInt8 busType);
  485.  
  486.     pascal Boolean        OTMakePortName(char* buffer, const char* moduleName,
  487.                                        OTPortRef);
  488.     
  489. #ifdef __cplusplus
  490. }
  491. #endif
  492.     
  493. /*******************************************************************************
  494. ** Some defines
  495. ********************************************************************************/
  496.  
  497. /*    -------------------------------------------------------------------------
  498.     Signals that are generated by a stream
  499.     ------------------------------------------------------------------------- */
  500.     
  501.     enum
  502.     {
  503.         SIGHUP        = 1,
  504.         SIGURG        = 16,
  505.         SIGPOLL        = 30
  506.     };
  507.  
  508. /*    -------------------------------------------------------------------------
  509.     IOCtls used by OpenTransport
  510.     ------------------------------------------------------------------------- */
  511.  
  512.     typedef int    OTNotifyType;
  513.     
  514.     enum
  515.     {
  516.         kOTNotifyAllModules = 0, kOTNotifyInterestedModules = 1,
  517.         kOTNotifyControlModules = 2
  518.     };
  519.     
  520.     struct OTIOCtlNotifyInfo
  521.     {
  522.         OTEventCode    fCode;        // Event Code
  523.         void*        fCookie;    // Cookie associated with it
  524.         UInt32        fNotifyType;// Who to notify
  525.     };
  526.     
  527.     typedef struct OTIoctlNotifyInfo    OTIoctlNotifyInfo;
  528.  
  529.     enum
  530.     {
  531.         I_OTNotifyAllClients    = MIOC_CMD(MIOC_OT, 50),
  532.         I_OTSetPowerLevel        = MIOC_CMD(MIOC_OT, 51)
  533.     };
  534.     
  535. /*    -------------------------------------------------------------------------
  536.     Some equates for kPROTOCOLEVENTs that normal clients don't need to 
  537.     know.
  538.     ------------------------------------------------------------------------- */
  539.  
  540.     enum
  541.     {
  542.         kHiPriProtocolEvent        = 0x08000000    /* Bit to be a high-priority event        */
  543.     };
  544.  
  545.     #define IsHiPriProtocolEvent(x)        (((x) & 0xf8000000) == (kPROTOCOLEVENT | kHiPriProtocolEvent))
  546.     #define StripProtocolEvent(x)        ((x) & 0xf0ffffff)
  547.     //
  548.     // Use this template to define your preferences if you use
  549.     // the TStreamGroup and TStreamFamily infrastructure
  550.     //
  551.     #define OTPreferencesFields(num)            \
  552.         UInt16            fVersion;                \
  553.         UInt16            fNumPrefs;                \
  554.         OTPortRef        fPort;                    \
  555.         OTLink            fLink;                    \
  556.         void*            fPrefs[num]
  557.     //
  558.     // This template is used to define the first set
  559.     // of fields in each individual preference array
  560.     //
  561.     #define OTPreferenceFields                    \
  562.         UInt16            fVersion;                \
  563.         UInt16            fSize
  564.         
  565.     //
  566.     // Define to tell infrastructure you want the main preference
  567.     // structure instead of a substructure.
  568.     //
  569.     enum
  570.     {
  571.         kOTPrefStructureCode = (UInt32)-1L
  572.     };
  573.     
  574. /*******************************************************************************
  575. ** Atomic Operations
  576. **
  577. ** The Bit operations return the previous value of the bit (0 or non-zero).
  578. ** The memory pointed to must be a single byte and only bits 0 through 7 are
  579. ** valid.  Bit 0 corresponds to a mask of 0x01, and Bit 7 to a mask of 0x80.
  580. ********************************************************************************/
  581.  
  582. typedef UInt8    OTLock;
  583.  
  584. #ifdef __cplusplus
  585. extern "C" {
  586. #endif
  587.  
  588. #if GENERATING68K
  589.  
  590.     #pragma parameter __D0 OTAtomicSetBit(__A0, __D0)
  591.     Boolean OTAtomicSetBit(UInt8*, size_t) =
  592.     {
  593.         0x01d0, 0x56c0,    /* bset.b d0,(a0); sne d0 */
  594.         0x7201, 0xc081    /* moveq #1,d1; and.l d1,d0 */
  595.     };
  596.         
  597.     #pragma parameter __D0 OTAtomicClearBit(__A0, __D0)
  598.     Boolean OTAtomicClearBit(UInt8*, size_t) =
  599.     {
  600.         0x0190, 0x56c0,    /* bclr.b d0,(a0); sne d0 */
  601.         0x7201, 0xc081    /* moveq #1,d1; and.l d1,d0 */
  602.     };
  603.         
  604.     #pragma parameter __D0 OTAtomicTestBit(__A0, __D0)
  605.     Boolean OTAtomicTestBit(UInt8*, size_t) =
  606.     {
  607.         0x0110, 0x56c0,    /* btst.b d0,(a0); sne d0 */
  608.         0x7201, 0xc081    /* moveq #1,d1; and.l d1,d0 */
  609.     };
  610.     
  611.     #pragma parameter __D0 OTCompareAndSwapPtr(__D0, __D1, __A0)
  612.     Boolean OTCompareAndSwapPtr(void*, void*, void**) =
  613.     {
  614.         0x0ed0, 0x0040,        /*    cas.l    d0,d1,(a0)    */
  615.         0x57c0,                /*    seq        d0            */
  616.         0x7201, 0xc081        /* moveq #1,d1; and.l d1,d0 */
  617.     };
  618.  
  619.     #pragma parameter __D0 OTCompareAndSwap32(__D0, __D1, __A0)
  620.     Boolean OTCompareAndSwap32(UInt32, UInt32, UInt32*) =
  621.     {
  622.         0x0ed0, 0x0040,        /*    cas.l    d0,d1,(a0)    */
  623.         0x57c0,                /*    seq        d0            */
  624.         0x7201, 0xc081        /* moveq #1,d1; and.l d1,d0 */
  625.     };
  626.     
  627.     #pragma parameter __D0 OTCompareAndSwap16(__D0, __D1, __A0)
  628.     Boolean OTCompareAndSwap16(UInt16, UInt16, UInt16*) =
  629.     {
  630.         0x0cd0, 0x0040,        /*    cas.w    d0,d1,(a0)    */
  631.         0x57c0,                /*    seq        d0            */
  632.         0x7201, 0xc081        /* moveq #1,d1; and.l d1,d0 */
  633.     };
  634.     
  635.     #pragma parameter __D0 OTCompareAndSwap8(__D0, __D1, __A0)
  636.     Boolean OTCompareAndSwap8(UInt8, UInt8, UInt8*) =
  637.     {
  638.         0x0ad0, 0x0040,        /*    cas.b    d0,d1,(a0)    */
  639.         0x57c0,                /*    seq        d0            */
  640.         0x7201, 0xc081        /* moveq #1,d1; and.l d1,d0 */
  641.     };
  642.  
  643. #elif GENERATINGPOWERPC
  644.  
  645.     Boolean OTAtomicSetBit(UInt8*, size_t);
  646.     Boolean OTAtomicClearBit(UInt8*, size_t);
  647.     Boolean OTAtomicTestBit(UInt8*, size_t);
  648.     //
  649.     // WARNING! void* and UInt32 locations MUST be on 4-byte boundaries.
  650.     //            UInt16 locations must not cross a 4-byte boundary.
  651.     //
  652.     Boolean    OTCompareAndSwapPtr(void* oldValue, void* newValue, void** location);
  653.     Boolean    OTCompareAndSwap32(UInt32 oldValue, UInt32 newValue, UInt32* location);
  654.     Boolean    OTCompareAndSwap16(UInt16 oldValue, UInt16 newValue, UInt16* location);
  655.     Boolean    OTCompareAndSwap8(UInt8 oldValue, UInt8 newValue, UInt8* location);
  656.  
  657. #endif
  658.     //
  659.     // WARNING! UInt32 locations MUST be on 4-byte boundaries.
  660.     //            UInt16 locations must not cross a 4-byte boundary.
  661.     //
  662.     SInt32    OTAtomicAdd32(SInt32, SInt32*);
  663.     SInt16    OTAtomicAdd16(SInt16, SInt16*);
  664.     SInt8    OTAtomicAdd8(SInt8, SInt8*);
  665.  
  666.     Boolean    OTAcquireLock(OTLock*);
  667.     
  668. #define OTClearLock(lockPtr)    *(lockPtr) = 0
  669. #define OTAcquireLock(lockPtr)    (OTAtomicSetBit(lockPtr, 0) == 0)
  670.  
  671. #ifdef __cplusplus
  672. }
  673. #endif
  674.  
  675. /*******************************************************************************
  676. ** Functions to convert xti/mac errors to OSStatus'
  677. ********************************************************************************/
  678.  
  679. #ifdef __cplusplus
  680. extern "C" {
  681. #endif
  682.  
  683. pascal OSStatus OTConvertError(OTXTIErr xtiErr, OTUnixErr macErr);
  684.                     
  685. #ifdef __cplusplus
  686. }
  687. #endif
  688.  
  689. #ifdef __cplusplus
  690. //
  691. // Macro for Speed
  692. //
  693. inline OSStatus OTDoConvertError(long xtiErr, long macErr)
  694. {
  695.     return xtiErr == 0 ? kOTNoError : OTConvertError((OTXTIErr)xtiErr, (OTUnixErr)macErr);
  696. }
  697.  
  698. #else
  699.  
  700. #define OTDoConvertError(xtiErr, macErr)    \
  701.     ((xtiErr) == 0 ? kOTNoError : OTConvertError((OTXTIErr)(xtiErr), (OTUnixErr)(macErr)))
  702.     
  703. #endif
  704.  
  705. /*******************************************************************************
  706. ** Some efficient implementations of BigEndian and LittleEndian words
  707. ********************************************************************************/
  708.  
  709. #if MI_BIG_ENDIAN
  710. //
  711. // Since Open Transport is 68020 and better only, we don't have to
  712. // worry about alignment problems.
  713. //
  714. #define GetBigEndian16At(ptr)        (*(UInt16*)(ptr))
  715. #define GetBigEndian32At(ptr)        (*(UInt32*)(ptr))    
  716. #define SetBigEndian16At(ptr, val)    { *(UInt16*)(ptr) = (UInt16)(val); }
  717. #define SetBigEndian32At(ptr, val)    { *(UInt32*)(ptr) = (UInt32)(val); }
  718.  
  719. #else
  720.  
  721. #define GetBigEndian16At(ptr)                        \
  722.         ((Uint16)(((((UInt8*)ptr)[0]) << 8) + ((UInt8*)ptr)[1]))
  723.  
  724. #define GetBigEndian32At(ptr)                        \
  725.         ((UInt32)(((((UInt8*)ptr)[0]) << 24) +        \
  726.                     (((UInt8*)ptr)[1]  << 16) +        \
  727.                     (((UInt8*)ptr)[2]  << 8) +        \
  728.                      ((UInt8*)ptr)[3]))
  729.  
  730. #define SetBigEndian16At(ptr, val)                \
  731.         {                                            \
  732.             ((UInt8*)ptr)[0] = (UInt8)(val >> 8);    \
  733.             ((UInt8*)ptr)[1] = (UInt8)(val);        \
  734.         }
  735.         
  736. #define SetBigEndian32At(ptr, val)                \
  737.         {                                            \
  738.             ((UInt8*)ptr)[0] = (UInt8)(val >> 24);    \
  739.             ((UInt8*)ptr)[1] = (UInt8)(val >> 16);    \
  740.             ((UInt8*)ptr)[2] = (UInt8)(val >> 8);    \
  741.             ((UInt8*)ptr)[3] = (UInt8)(val);        \
  742.         }
  743.  
  744. #endif
  745.         
  746. #if MI_LITTLE_ENDIAN
  747.  
  748. #define GetLittleEndian16At(ptr)            (*(UInt16*)(ptr))
  749. #define GetLittleEndian32At(ptr)            (*(UInt32*)(ptr))    
  750. #define SetLittleEndian16At(ptr, val)    { *(UInt16*)(ptr) = (UInt16)(val); }
  751. #define SetLittleEndian32At(ptr, val)    { *(UInt32*)(ptr) = (UInt32)(val); }
  752.  
  753. #else
  754.  
  755. #define GetLittleEndian16At(ptr)                    \
  756.         ((Uint16)(((((UInt8*)ptr)[1]) << 8) + ((UInt8*)ptr)[0]))
  757.  
  758. #define GetLittleEndian32At(ptr)                    \
  759.         ((UInt32)(((((UInt8*)ptr)[3]) << 24) +        \
  760.                     (((UInt8*)ptr)[2]  << 16) +        \
  761.                     (((UInt8*)ptr)[1]  << 8) +        \
  762.                      ((UInt8*)ptr)[0]))
  763.  
  764. #define SetLittleEndian16At(ptr, val)                \
  765.         {                                            \
  766.             ((UInt8*)ptr)[1] = (UInt8)(val >> 8);    \
  767.             ((UInt8*)ptr)[0] = (UInt8)(val);        \
  768.         }
  769.         
  770. #define SetLittleEndian32At(ptr, val)                \
  771.         {                                            \
  772.             ((UInt8*)ptr)[3] = (UInt8)(val >> 24);    \
  773.             ((UInt8*)ptr)[2] = (UInt8)(val >> 16);    \
  774.             ((UInt8*)ptr)[1] = (UInt8)(val >> 8);    \
  775.             ((UInt8*)ptr)[0] = (UInt8)(val);        \
  776.         }
  777.  
  778. #endif
  779.  
  780. #ifdef __cplusplus
  781.  
  782. /*    -------------------------------------------------------------------------
  783.     TOTBaseObject
  784.     
  785.     This is the base class for Open Transport objects that DO NOT need to 
  786.     be 68K aligned.
  787.     ------------------------------------------------------------------------- */
  788.  
  789. #if USESINGLEOBJECT
  790.     class TOTBaseObject : public SingleObject
  791. #else
  792.     class TOTBaseObject
  793. #endif
  794.     {
  795.         EXTRA_VTABLE_SLOT
  796.         
  797.         public:
  798.                             _CT TOTBaseObject() {};
  799.             virtual            _DT TOTBaseObject();
  800.             
  801.         private:
  802.                             _CT TOTBaseObject(const TOTBaseObject&);
  803.                     void    operator=(const TOTBaseObject&);
  804.     };
  805.     
  806. #if GENERATINGPOWERPC
  807. #pragma options align=mac68k
  808. #endif
  809.  
  810. /*    -------------------------------------------------------------------------
  811.     TOTAlignedObject
  812.  
  813.     This is the base class for Open Transport objects that DO need to 
  814.     be 68K aligned.
  815.     ------------------------------------------------------------------------- */
  816.  
  817. #if USESINGLEOBJECT
  818.     class TOTAlignedObject : public SingleObject
  819. #else
  820.     class TOTAlignedObject
  821. #endif
  822.     {
  823.         EXTRA_VTABLE_SLOT
  824.         
  825.         public:
  826.                             _CT TOTAlignedObject() {};
  827.             virtual            _DT TOTAlignedObject();
  828.  
  829.         private:
  830.                             _CT TOTAlignedObject(const TOTAlignedObject&);
  831.                     void    operator=(const TOTAlignedObject&);
  832.     };
  833.  
  834. #if GENERATINGPOWERPC
  835. #pragma options align=reset
  836. #endif
  837.  
  838. #endif
  839.  
  840. #endif    /* __OPENTPTDEV__    */
  841.